package cn.okcode.slowjson;


public class MatchHelper {

    public static final String LEFT_BRACKET = "(";
    public static final String RIGHT_BRACKET = ")";
    public static final String COMMA = ",";
    public static final String LEFT_SQUARE = "[";
    public static final String RIGHT_SQUARE = "]";

    /**
     * 匹配范围
     *
     * @param range 范围(3,8) (3,8] [3,8) [3,8]
     * @param value 被匹配值
     * @return 匹配结果，true-在范围内；false-不再范围内
     */
    public boolean matchRange(String range, String value) {
        double dValue = parseDouble(value);

        String error = "范围“" + range + "”格式不对，正确格式示例：(4,8.9]";
        String[] arr = range.split(COMMA);
        if (arr.length != 2) {
            throw new RuntimeException(error);
        }

        String left = arr[0];
        String right = arr[1];

        if (left.length() < 2 || right.length() < 2) {
            throw new RuntimeException(error);
        }

        //比较左侧
        String leftSignal = left.substring(0, 1);
        String leftValue = left.substring(1);
        System.out.println("leftS:" + leftSignal + " leftValue:" + leftValue);
        double dLeftValue = parseDouble(leftValue);
        if (LEFT_BRACKET.equals(leftSignal)) {
            if (dValue <= dLeftValue) {
                return false;
            }
        }
        if (LEFT_SQUARE.equals(leftSignal)) {
            if (dValue < dLeftValue) {
                return false;
            }
        }

        //比较右侧
        String rightSignal = right.substring(right.length() - 1);
        String rightValue = right.substring(0, right.length() - 1);
        System.out.println("rightSignal:" + rightSignal + " rightValue:" + leftValue);
        double dRightValue = parseDouble(rightValue);
        if (RIGHT_BRACKET.equals(rightSignal)) {
            if (dValue >= dRightValue) {
                return false;
            }
        }
        if (RIGHT_SQUARE.equals(rightSignal)) {
            if (dValue > dRightValue) {
                return false;
            }
        }
        return true;
    }

    private double parseDouble(String value) {
        try {
            return Double.parseDouble(value);
        } catch (NumberFormatException e) {
            throw new RuntimeException("字符串“" + value + "”不能转换为数值！");
        }
    }

    public static void main(String[] args) {
        System.out.println(new MatchHelper().matchRange("(5.123,20.889]", "20.889"));
        //System.out.println(new MatchHelper().matchRange("(3,8]", "5"));
        //System.out.println(new MatchHelper().matchRange("(3,8]", "3"));
        //System.out.println(new MatchHelper().matchRange("(3,8]", "8"));
        //System.out.println(new MatchHelper().matchRange("(3,8.98]", "8.5"));
        //System.out.println(new MatchHelper().matchRange("(3,8]", "1"));
        //System.out.println(new MatchHelper().matchRange("3,8]", "1"));
        //System.out.println(new MatchHelper().matchRange("[3]", "1"));
        //System.out.println(new MatchHelper().matchRange("[3,8", "1"));
        //System.out.println(new MatchHelper().matchRange("[3,]", "1"));
    }
}
